﻿
/*
 *
 * Copyright (C) 2009 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System.IO;
using System.Xml;
using System.Collections.Generic;
using FlowLib.Containers;

namespace ClientExample.Utils
{
    public class XmlList<T> where T:new()
    {
        protected XmlDocument document = null;
        protected XmlElement xmlCurrent = null;
        protected XmlElement xmlParent = null;
        protected T itemCurrent = default(T);

        protected FlowSortedList<T> list = new FlowSortedList<T>();

        public virtual FlowSortedList<T> Items
        {
            get { return list; }
            set { list = new FlowSortedList<T>(value, value.Comparer); }
        }

        public string TopNodeName
        {
            get;
            set;
        }

        public XmlList()
        {
            TopNodeName = "TopNode";
        }

        public virtual void Read(byte[] data)
        {
            document = new XmlDocument();
            string xml = System.Text.Encoding.UTF8.GetString(data);
            xml = xml.Replace("\x001", "LOWCHAR001");
            if (xml.IndexOf('<') == 1)
                xml = xml.Remove(0, 1);
            document.LoadXml(xml);
            XmlNodeList xmlList = document.GetElementsByTagName(TopNodeName);
            if (xmlList != null && xmlList.Count == 1)
            {
                xmlList = xmlList[0].ChildNodes;
                for (int i = 0; i < xmlList.Count; i++)
                {
                    NewNode(xmlList[i].Name, false);
                    foreach (XmlAttribute var in xmlList[i].Attributes)
                    {
                        NodeInfo(var.Name, var.Value, false);
                    }
                    EndNode(false);
                }
            }
        }

        public virtual byte[] Write()
        {
            document = new XmlDocument();
            document.AppendChild(document.CreateXmlDeclaration("1.0", System.Text.Encoding.UTF8.BodyName, "yes"));
            //XmlElement xmlRoot = document.CreateElement("root");
            xmlParent = xmlCurrent = document.CreateElement(TopNodeName);
            foreach (T item in list)
            {
                NodeInfo(item, true);
            }
            //xmlRoot.AppendChild(xmlParent);
            //document.AppendChild(xmlRoot);
            document.AppendChild(xmlParent);

            MemoryStream ms = new MemoryStream();
            document.Save(ms);
            return ms.ToArray();
        }

        protected virtual void NewNode(string name, bool isSave)
        {
            if (isSave)
            {
                xmlParent = xmlCurrent;
                xmlCurrent = document.CreateElement(name);
            }
            else
            {
                itemCurrent = new T();
            }
        }

        protected virtual void EndNode(bool isSave)
        {
            if (isSave)
            {
                xmlParent.AppendChild(xmlCurrent);
                xmlCurrent = xmlParent;
                if (xmlParent.ParentNode != null)
                {
                    xmlParent = (XmlElement)xmlParent.ParentNode;
                }
            }
            else
            {
                list.Add(itemCurrent);
            }
        }
        protected virtual void NodeInfo(string key, string value, bool isSave)
        {
            if (isSave)
            {
                xmlCurrent.SetAttribute(key, value);
            }
            // Do not handle Else
        }

        protected virtual void NodeInfo(T item, bool isSave)
        {
            if (isSave)
            {
                NewNode(typeof(T).Name, isSave);
                xmlCurrent.InnerText = item.ToString();
                EndNode(isSave);
            }
        }
    }
}
